package com.cango.student.algorithm.leetcode;

import com.cango.student.algorithm.nodepackage.Node;

/**
 * linked-list-cycle
 */
public class IsRound {
    /**
     * 链表中环检测
     *
     */
    public static boolean isRound(Node node) {
        Node fast = node.next;
        Node slow = node;
        while (true) {
            if (fast.next == null || fast.next.next == null) {
                return false;
            }
            fast = fast.next.next;
            slow = slow.next;

            if (fast.data == slow.data)
                return true;

        }
    }

    public static void main(String[] args) {
        Node linkNode1 = new Node();
        linkNode1.data = 1;
        Node linkNode2 = new Node();
        linkNode2.data = 2;
        Node linkNode3 = new Node();
        linkNode3.data = 3;
//        Node linkNode4 = new Node();
//        linkNode4.data = 4;
//        Node linkNode5 = new Node();
//        linkNode5.data = 5;
//        Node linkNode6 = new Node();
//        linkNode6.data = 6;

        linkNode1.next = linkNode2;
        linkNode2.next = linkNode3;
//        linkNode3.next = linkNode4;
//        linkNode4.next = linkNode5;
//        linkNode5.next = linkNode6;
//        linkNode6.next = linkNode1;

        System.out.println(isRound(linkNode1));

    }
}
